package cn.caishen.report.controller;

import cn.caishen.report.common.exceptions.BusinessException;
import cn.caishen.report.common.exceptions.ResponseResult;
import cn.caishen.report.common.utils.IntegerUtil;
import cn.caishen.report.common.utils.LbMap;
import cn.caishen.report.common.utils.StringUtil;
import cn.caishen.report.domain.report.ReportUserField;
import cn.caishen.report.service.report.ReportUserFieldService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 报表用户字段控制层
 */
@RestController
@RequestMapping(value = "/reportUserField")
@Slf4j
@Api(tags = "报表用户字段")
public class ReportUserFieldController {

    private final ReportUserFieldService reportUserFieldService;

    public ReportUserFieldController(ReportUserFieldService reportUserFieldService) {
        this.reportUserFieldService = reportUserFieldService;
    }

    /**
     * 报表用户字段分页查询列表
     *
     * @param page    页码
     * @param limit   单页条数
     * @param jsonStr 查询条件
     * @return LbMap
     */
    //@GetMapping(value = "/list", name = "报表用户字段分页查询列表")
    //@ApiOperation("报表用户字段分页查询列表")
    public LbMap list(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit, @RequestParam(required = false) String jsonStr) {
        try {
            LbMap param = LbMap.fromObject(jsonStr);

            if (IntegerUtil.isEmpty(param.getInt("reportConfigId"))) {
                throw new BusinessException("必须选择一张报表进行查询");
            }

            PageInfo<ReportUserField> pages = reportUserFieldService.getList(page, limit, param);
            log.info("报表用户字段分页查询列表成功");
            return ResponseResult.success("报表用户字段分页查询列表成功", pages.getList(), pages.getTotal());
        } catch (BusinessException e) {
            log.info("报表用户字段分页查询列表失败：" + e.getMessage());
            return ResponseResult.fail("报表用户字段分页查询列表失败：" + e.getMessage());
        } catch (Exception e) {
            log.info("报表用户字段分页查询列表失败：" + e.getMessage());
            return ResponseResult.fail("报表用户字段分页查询列表失败");
        }
    }

    /**
     * 修改报表用户字段（新增、修改）
     *
     * @param reportUserFieldList reportUserFieldList
     * @return LbMap
     */
    //@PostMapping(value = "/edit", name = "修改报表用户字段")
    //@ApiOperation("修改报表用户字段（新增、修改）")
    public LbMap edit(@RequestBody List<ReportUserField> reportUserFieldList) {
        try {
            for (ReportUserField reportUserField : reportUserFieldList) {
                if (IntegerUtil.isEmpty(reportUserField.getReportConfigId())) {
                    throw new BusinessException("报表配置编号不能为空");
                }

                if (IntegerUtil.isEmpty(reportUserField.getFieldWidth())) {
                    throw new BusinessException("字段长度不能为空");
                }

                if (StringUtil.isEmpty(reportUserField.getFieldName())) {
                    throw new BusinessException("字段名称不能为空");
                }

                if (Objects.isNull(reportUserField.getReportUserFieldId()) || reportUserField.getReportUserFieldId() <= 0) {
                    //新增
                    reportUserFieldService.saveOne(reportUserField);
                    log.info("报表用户字段新增成功");
                } else {
                    //编辑
                    reportUserFieldService.update(reportUserField);
                    log.info("报表用户字段修改成功");
                }
            }
            return ResponseResult.success("报表用户字段编辑成功");
        } catch (BusinessException e) {
            log.info("报表用户字段编辑失败：" + e.getMessage());
            return ResponseResult.fail("报表用户字段编辑失败：" + e.getMessage());
        } catch (Exception e) {
            log.info("报表用户字段编辑失败：" + e.getMessage());
            return ResponseResult.fail("报表用户字段编辑失败");
        }
    }

    /**
     * 根据id删除报表用户字段
     *
     * @param primaryKey id
     * @return LbMap
     */
    //@PostMapping(value = "/delete", name = "删除报表用户字段")
    //@ApiOperation("删除报表用户字段")
    public LbMap delete(String primaryKey) {
        try {
            if (Objects.isNull(primaryKey) || primaryKey.length() == 0) {
                return ResponseResult.fail("没有找到报表用户字段编号");
            }
            reportUserFieldService.delete(primaryKey);
            log.info("报表用户字段删除成功");
            return ResponseResult.success("报表用户字段删除成功");
        } catch (BusinessException e) {
            log.info("报表用户字段删除失败：" + e.getMessage());
            return ResponseResult.fail("报表用户字段删除失败：" + e.getMessage());
        } catch (Exception e) {
            log.info("报表用户字段删除失败：" + e.getMessage());
            return ResponseResult.fail("报表用户字段删除失败");
        }
    }


    /**
     * 保存用户自定义列
     *
     * @param reportUserFieldList reportUserFieldList
     * @return LbMap
     */
    @PostMapping(value = "/saveUserField", name = "保存用户自定义列")
    @ApiOperation("保存用户自定义列")
    public LbMap saveUserField(@RequestBody List<ReportUserField> reportUserFieldList) {
        try {
            Integer reportConfigId = 0;
            for (ReportUserField reportUserField : reportUserFieldList) {
                if (IntegerUtil.isEmpty(reportConfigId)) {
                    reportConfigId = reportUserField.getReportConfigId();
                }

                if (IntegerUtil.isEmpty(reportUserField.getReportConfigId())) {
                    throw new BusinessException("报表配置编号不能为空");
                }

                if (!reportConfigId.equals(reportUserField.getReportConfigId())) {
                    throw new BusinessException("报表配置编号不能为多个");
                }

                if (IntegerUtil.isEmpty(reportUserField.getFieldWidth())) {
                    throw new BusinessException("字段长度不能为空");
                }

                if (StringUtil.isEmpty(reportUserField.getFieldName())) {
                    throw new BusinessException("字段名称不能为空");
                }
            }

            reportUserFieldService.saveUserField(reportConfigId, reportUserFieldList);
            log.info("保存用户自定义列成功");
            return ResponseResult.success("保存用户自定义列成功");
        } catch (BusinessException e) {
            log.info("保存用户自定义列失败：" + e.getMessage());
            return ResponseResult.fail("保存用户自定义列失败：" + e.getMessage());
        } catch (Exception e) {
            log.info("保存用户自定义列失败：", e);
            return ResponseResult.fail("保存用户自定义列失败");
        }
    }
}